require(["./config"], () => {
  require([
    "request",
    "template",
    "header",
    "footer",
    "bootstrap",
    "ddb",
    "land",
    "jquery",
    "zoom",
  ], (req, tmp) => {
    class Detail {
      constructor() {
        this.count = 2 - 1;
        this.init();
      }

      init() {
        const id = window.location.search.split("=")[1];
        // console.log(id);
        this.getDate(id);
      }
      getDate(id) {
        req.getdetData(id).then((res) => {
          if (res.code === 200) {
            this.renderDel(res.data.detail);
          }
        });
      }
      renderDel(obj) {
        const { id, image, photo, title, price, saleNum, descContentList } =
          obj;

        const html = tmp("product_det", {
          id,
          image,
          photo,
          title,
          price,
          saleNum,
          descContentList,
        });

        $(".product_intro").html(html);
        this.zoom();
        this.addReduce();
        this.addToCart({ id, image, title, price });
        this.ddJin();
      }

      // 数量加减
      addReduce() {
        // 给按钮绑定事件
        $(".reduce").click(() => {
          this.count--;
          $(".input_detail_main_buy_num").val(this.count);
          if ((this.count = 1)) {
            this.count = 2;
          }
        });
        $(".plus").click(() => {
          this.count++;
          $(".input_detail_main_buy_num").val(this.count);
        });
      }
      // 加入购物车
      addToCart(obj) {
        const { id, image, title, price } = obj;
        // console.log({id,image,title,price});
        $(".add").click(() => {
          // console.log(123);
          // 1:去localStorage 取数据
          let prodList = localStorage.getItem("prodList");
          if (prodList) {
            // 有数据
            // console.log(prodList);
            prodList = JSON.parse(prodList);
            // 判断数组里面有没有这条数据返回值是布尔值
            const flag = prodList.some((item) => {
              return item.id === id;
            });
            if (flag) {
              // 有这条商品 数量相加
              prodList.forEach((item) => {
                if (item.id === id) {
                  item.count += this.count;
                }
              });
            } else {
              // 没有这条商品就push到数组里面
              prodList.push({
                id,
                image,
                title,
                price,
                count: this.count,
                isChecked: true,
              });
            }

            // 在存一下数据
            localStorage.setItem("prodList", JSON.stringify(prodList));
          } else {
            // 没有数据直接存
            localStorage.setItem(
              "prodList",
              JSON.stringify([
                { id, image, title, price, count: this.count, isChecked: true },
              ])
            );
          }
          $(".gou").fadeIn(1000, () => {
            $(".gou").fadeOut(1000);
          });
        });
        header.computedCount();
      }
      // 放大镜
      zoom() {
        $(".zoom-image").elevateZoom();
        const smDm = document.querySelectorAll(".curPreview img"),
          bmDm = document.querySelector(".zoom-image");

        // console.log(bmDm);
        // console.log(smDm);
        for (let i = 0; i < smDm.length; i++) {
          smDm[i].onmouseenter = function () {
            const src = this.src;
            bmDm.src = src;
            // console.log(src);
          };
        }
      }
    }

    return new Detail();
  });
});
